package com.ict.ms.controller;

import com.ict.ms.userLogin.facade.api.IUserInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ict.ms.userLogin.facade.model.UserInfo;
import com.ict.ms.common.StateMsg;
import javax.annotation.Resource;
import java.util.List;

/**
 * Created by chenlong on 2016/10/28.
 */
@Controller
@RequestMapping({"/user"})
public class UserController {
    @Resource
    IUserInfoService userInfoService;

    /**
     * 用户登录
     * @param map
     * @param userName 用户名
     * @param passWord 密码
     * @return json格式登录成功或失败状态信息
     */
    @ResponseBody
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    public String listMMSI(ModelMap map,@RequestParam(value="userName", required = false) String userName,
                           @RequestParam(value="password", required = false) String passWord,@RequestParam(value="type", required = false) int type){

        String msg;
        String jsonStr;
        UserInfo userInfo=userInfoService.getUserInfo(userName,passWord);
        if(userInfo==null){
            msg="用户名不存在";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }

        if(!userInfo.getPassWord().equals(passWord)){
            msg="密码错误";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-2, map);
            return jsonStr;
        }

        //Web登录
        if(type==0){
            if(userInfo.getPermission()<1){
                msg="用户权限不够";
                map.addAttribute("result",msg);
                jsonStr = StateMsg.toJson(-4, map);
                return jsonStr;
            }
        }
        //2D登录
        else if(type==1){
            if(userInfo.getPermission()<2){
                msg="用户权限不够";
                map.addAttribute("result",msg);
                jsonStr = StateMsg.toJson(-4, map);
                return jsonStr;
            }
        }else{
            msg="参数值传递错误";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-3, map);
            return jsonStr;
        }

        map.addAttribute("userName",userInfo.getUserName());
        map.addAttribute("passWord",userInfo.getPassWord());
        map.addAttribute("name",userInfo.getName());
        map.addAttribute("department",userInfo.getDepartment());
        map.addAttribute("position",userInfo.getPosition());
        map.addAttribute("userName",userInfo.getUserName());
        map.addAttribute("permission",userInfo.getPermission());

        jsonStr = StateMsg.toJson(1, map);
        return jsonStr;
    }



    /**
     * 添加用户
     * @param map
     * @param userName 用户名
     * @param passWord 密码
     * @return 添加成功或者失败状态Json格式数据
     */
    @ResponseBody
    @RequestMapping(value = "add.do",method = RequestMethod.POST)
    public String userAdd(ModelMap map,@RequestParam(value="userName", required = false) String userName,
                           @RequestParam(value="password", required = false) String passWord,
                          @RequestParam(value="name", required = false) String name,
                          @RequestParam(value="department", required = false) String department,
                          @RequestParam(value="position", required = false) String position,
                          @RequestParam(value="permission", required = false) int permission){
        String msg;
        String jsonStr;

        UserInfo userInfo=new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setPassWord(passWord);
        userInfo.setDepartment(department);
        userInfo.setName(name);
        userInfo.setPosition(position);
        userInfo.setPermission(permission);

        if(passWord.length()<6){
            msg="添加用户失败，密码不符合规范";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-2, map);
            return jsonStr;
        }

        boolean ifSuccess=userInfoService.userAdd(userInfo);
        if(!ifSuccess){
            msg="添加用户失败，请检查用户名是否已存在";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }

        msg="用户添加成功";
        map.addAttribute("result",msg);
        jsonStr = StateMsg.toJson(1, map);
        return jsonStr;
    }

    @ResponseBody
    @RequestMapping(value = "delete.do",method = RequestMethod.POST)
    public String userDelete(ModelMap map,@RequestParam(value="userName", required = false) String userName){
        String msg;
        String jsonStr;

        boolean ifSuccess=userInfoService.userDelete(userName);
        if(!ifSuccess){
            msg="删除用户失败";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }

        msg="删除成功";
        map.addAttribute("result",msg);
        jsonStr = StateMsg.toJson(1, map);
        return jsonStr;
    }

    /**
     * 修改用户（当前只提供修改密码）
     * @param map
     * @param userName 要修改的用户名
     * @param passWord 修改后的密码
     * @return 修改成功或者失败状态Json格式数据
     */
    @ResponseBody
    @RequestMapping(value = "update.do",method = RequestMethod.POST)
    public String userUpdate(ModelMap map,@RequestParam(value="userName", required = false) String userName,
                             @RequestParam(value="password", required = false) String passWord,
                             @RequestParam(value="name", required = false) String name,
                             @RequestParam(value="department", required = false) String department,
                             @RequestParam(value="position", required = false) String position,
                             @RequestParam(value="permission", required = false) int permission){
        String msg;
        String jsonStr;

        UserInfo userInfo=new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setPassWord(passWord);
        userInfo.setDepartment(department);
        userInfo.setName(name);
        userInfo.setPosition(position);
        userInfo.setPermission(permission);

        if(passWord.length()<6){
            msg="修改失败，密码不符合规范";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-2, map);
            return jsonStr;
        }

        boolean ifSuccess=userInfoService.userUpdate(userInfo);
        if(!ifSuccess){
            msg="修改用户信息失败";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }

        msg="用户修改成功";
        map.addAttribute("result",msg);
        jsonStr = StateMsg.toJson(1, map);
        return jsonStr;
    }

    @ResponseBody
    @RequestMapping(value = "getuser.do",method = RequestMethod.GET)
    public String userAllGet(ModelMap map){
        String msg;
        String jsonStr;

        List<UserInfo> userInfoList=userInfoService.getAllUser();
        if(userInfoList.size()==0){
            msg="获取用户信息失败";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }

        map.addAttribute("userList",userInfoList);
        jsonStr = StateMsg.toJson(1, map);
        return jsonStr;
    }


    /**
     * 用户修改密码-2D
     * @param map
     * @param userName
     * @param passWord
     * @param newpassWord
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "changepw.do",method = RequestMethod.POST)
    public String changePW(ModelMap map,@RequestParam(value="userName", required = false) String userName,
                           @RequestParam(value="password", required = false) String passWord,
                           @RequestParam(value="newpassword", required = false) String newpassWord){

        String msg;
        String jsonStr;

        if(newpassWord.length()<6){
            msg="修改失败，密码不符合规范";
            map.addAttribute("result",msg);
            jsonStr = StateMsg.toJson(-2, map);
            return jsonStr;
        }

       int state=userInfoService.changePW(userName,passWord,newpassWord);
        if(state==-1){
            msg="用户不存在";
        }else if(state==-2){
            msg="密码错误";
        }else {
            msg="密码修改成功";
        }
        map.addAttribute("result",msg);
        jsonStr = StateMsg.toJson(state, map);
        return jsonStr;
    }
}